package dataProcess;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import dataProcess.StringSpliter;

public class simple {
	public static void main(String args[]){
		try {
			Workbook wb = Workbook.getWorkbook(new File("data/input/Android_121224.xls"));
			Sheet sheet = wb.getSheet(0);
			int rowCount = sheet.getRows();
			StringSpliter ss = StringSpliter.getDefaultSpliter();
			//7 kinds of bugs
			String[] kind = {"Functionality", "Security", "Usability", "Reliability", "Efficiency", "Maintainability", "Portability"};
			//index: 0 ~ 6
			ArrayList<ArrayList<String>> example = new ArrayList<ArrayList<String>>();
			for (int i = 0; i < kind.length; i++) {
				example.add(new ArrayList<String>());
			}

			for(int i=0; i<rowCount; i++){
				String type = sheet.getCell(6, i).getContents();
				if(type.equals("Defect")){
					String label = sheet.getCell(0, i).getContents();
					String textInfo = sheet.getCell(5, i).getContents() + "\t" + sheet.getCell(9, i).getContents();
					textInfo = ss.simpleSplitSentenceIntoWords(textInfo);
					switch(label){
					case "Functionality" :						example.get(0).add(textInfo);						break;
					case "Security" :									example.get(1).add(textInfo);					    break;
					case "Useability" :    						    example.get(2).add(textInfo);					    break;
					case "Usability" :						        example.get(2).add(textInfo);						break;
					case "Reliability" :						        example.get(3).add(textInfo);						break;
					case "Efficiency" :						        example.get(4).add(textInfo);				        break;
					case "Maintainability":                    example.get(5).add(textInfo);                        break;
					case "Portability" :                            example.get(6).add(textInfo);                       break;
					default : break;
					}
				}
			}			
			System.out.println("Functionality: "+example.get(0).size() + "Security :"+example.get(1).size() + 
						"Usability :"+example.get(2).size() + "Reliability :"+example.get(3).size() + "Efficiency :"+example.get(4).size() +
						"Maintainability :"+example.get(5).size() 	+ "Portability" + example.get(6).size());
			
			for (int i = 0; i < kind.length; i++) {
				String className = kind[i];
				File category = new File("forWeka/" + className);
				if(category.exists())
					category.delete();
				category.mkdir();
				ArrayList<String> fileSet = example.get(i);
				for(int j=0; j<fileSet.size(); j++){
					FileWriter fw = new FileWriter("forWeka/" + className+"/"+j+".txt");
					fw.write(fileSet.get(j));
					fw.close();				
				}
			}
		
		} catch (BiffException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
